package com.turing.post.salesmanage.impl;

import java.math.BigInteger;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.account.util.MasterAccount;
import com.turing.post.account.util.MasterAccountDao;
import com.turing.post.cardmanagement.util.ActivateCard;
import com.turing.post.cardmanagement.util.ActivateCardDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.salesmanage.util.RedempOrder;
import com.turing.post.salesmanage.util.RedempOrderDao;
import com.turing.post.salesmanage.util.RedempOrderDetail;
import com.turing.post.salesmanage.util.RedempOrderDetailBusiness;
import com.turing.post.salesmanage.util.RedempOrderDetailDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 赎回单明细 （RedempOrderDetail）
 * 
 * @author zxp
 * @Jan 29, 2013
 */
@Stateless(mappedName = "salesmanage.impl.RedempOrderDetailBusinessImpl")
@Remote
@Local
public class RedempOrderDetailBusinessImpl implements RedempOrderDetailBusiness {

	@EJB(mappedName = "salesmanage.impl.RedempOrderDaoImpl")
	private RedempOrderDao redempOrderDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	@EJB(mappedName = "salesmanage.impl.RedempOrderDetailDaoImpl")
	private RedempOrderDetailDao redempOrderDetailDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.ActivateCardDaoImpl")
	private ActivateCardDao acDao;

	@EJB(mappedName = "account.impl.MasterAccountDaoImpl")
	private MasterAccountDao masterAccountDaoImpl;

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new SalesManageApplicationException(
				SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
				SalesManageRespCode.DEPOSIT_EXCEPTIONCODE, error);
	}

	@Override
	public void insertRedempOrderDetailInfo(PCPSWebInput webInput,
			RedempOrderDetail rod) throws PCPSApplicationException {
		if (webInput == null) {
			errException("用户登录对象为空!");
		}
		if (rod == null) {
			errException("赎回单明细单对象为空!");
		}
		if (rod.getRedempOrderId() == null) {
			errException("赎回单编号为空!");
		}
		if (PCPSUtil.isEmpty(rod.getCardNo())) {
			errException("卡号为空!");
		}
		if (rod.getRedempAmount() == null
				|| rod.getRedempAmount().compareTo(BigInteger.ZERO) <= 0) {
			errException("赎回金额输入错误!");
		}
		RedempOrder _redOrder = redempOrderDaoImpl.getRedempOrderForUpdate(rod
				.getRedempOrderId());
		if (_redOrder == null) {
			errException("待添加明细赎回单未找到!");
		}
		if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Input)) {
			errException("赎回单明细只有在录入状态才可添加!");
		}
		// 验证卡信息
		ActivateCard ac = acDao.getActivateCardByCardNo(rod.getCardNo());
		if (ac == null) {
			errException("卡" + rod.getCardNo() + "为无效卡!");
		}
		// 验证主账号
		MasterAccount ma = masterAccountDaoImpl.getMasterAccountByCardNo(rod
				.getCardNo());
		if (ma == null) {
			errException("未找到卡" + rod.getCardNo() + "主账号!");
		}
		if (rod.getFee() == null
				|| rod.getFee().compareTo(BigInteger.ZERO) <= 0) {
			errException("赎回费手续费为空或小于0!");
		}
		if (rod.getFactRedempAmount() == null
				|| rod.getFactRedempAmount().compareTo(BigInteger.ZERO) <= 0) {
			errException("实际赎回金额为空或小于0!");
		}
		// 判断金额
		// if (rod.getRedempAmount().compareTo(ac.getBalance()) > 0) {
		// errException("赎回金额超过卡余额!");
		// }
		// 判断是否存在
		RedempOrderDetail existRod = redempOrderDetailDaoImpl
				.getRedempOrderDetailByRedempOrderDetail(rod);
		if (existRod != null) {
			errException("卡" + rod.getCardNo() + "赎回单明细已添加!");
		}
		rod.setMasterAccNo(ma.getMasterAccNo());// 主帐号 int Not Null
		rod.setBeforeBalance(ac.getBalance());// 原余额 decimal(16) Not Null

		// rod.setBalance(BigInteger.ZERO);// 赎回后余额 decimal(16) Not Null >= 0
		rod.setOwnerId(ac.getOwnerId());
		redempOrderDetailDaoImpl.insertRedempOrderDetail(rod);
		// 更新_redOrder
		_redOrder.setTotalRedempAmount(_redOrder.getTotalRedempAmount().add(
				rod.getRedempAmount()));
		// System.out.println("rod.getFee()"+rod.getFee());
		// System.out.println("rod.getFactRedempAmount()"+rod.getFactRedempAmount());
		// PCPSUtil.Log("rod.getFee()"+rod.getFee());
		// PCPSUtil.Log("rod.getFactRedempAmount()"+rod.getFactRedempAmount());
		_redOrder.setTotalFee(_redOrder.getTotalFee().add(rod.getFee()));// 总手续费
		_redOrder.setTotalFactRedempAmount(_redOrder.getTotalFactRedempAmount()
				.add(rod.getFactRedempAmount()));// 总实际赎回金额 decimal(16) Null
		redempOrderDaoImpl.update(_redOrder);
		// 插入日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "新增赎回单明细", "赎回单编号 "
						+ _redOrder.getRedempOrderId());
	}

	@Override
	public void deleteRedempOrderDetailInfo(PCPSWebInput webInput,
			RedempOrderDetail rod) throws PCPSApplicationException {
		if (rod.getRedempOrderId() == null) {
			errException("赎回单编号为空!");
		}
		if (rod.getMasterAccNo() == null) {
			errException("主账号为空!");
		}
		RedempOrder _redOrder = redempOrderDaoImpl
				.getRedempOrderByRedempOrderId(rod.getRedempOrderId());
		if (_redOrder == null) {
			errException("待删除明细赎回单未找到!");
		}
		if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Input)) {
			errException("赎回单明细只有在录入状态才可删除!");
		}
		// 判断是否存在
		RedempOrderDetail existRod = redempOrderDetailDaoImpl
				.getRedempOrderDetailByRedempOrderDetail(rod);
		if (existRod == null) {
			errException("赎回单" + rod.getRedempOrderId() + "明细不存在!");
		}
		redempOrderDetailDaoImpl.deleteRedempOrderDetail(existRod);
		// 更新_redOrder
		_redOrder.setTotalRedempAmount(_redOrder.getTotalRedempAmount()
				.subtract(existRod.getRedempAmount()));

		_redOrder.setTotalFee(_redOrder.getTotalFee().subtract(
				existRod.getFee()));// 总手续费 decimal(16) Null
		_redOrder.setTotalFactRedempAmount(_redOrder.getTotalFactRedempAmount()
				.subtract(existRod.getFactRedempAmount()));// 总实际赎回金额

		// 插入日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Delete, "删除赎回单明细", "赎回单编号 "
						+ _redOrder.getRedempOrderId());
		redempOrderDaoImpl.update(_redOrder);
	}
}
